package priv.mill.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import priv.mill.controller.util.RetenObj;
import priv.mill.entry.RolePower;
import priv.mill.server.RolePowerService;

@Controller
@RequestMapping("RolePower")
public class RolePowerController {

	@Autowired
	private RolePowerService rolePowerService;

	private static final Logger logger = Logger.getLogger(RolePowerController.class);

	@RequestMapping("getRolePowerById")
	@ResponseBody
	public RetenObj getRolePowerById(@RequestParam("RolePowerId") Integer rolePowerId) {
		try {
			return warpData(rolePowerService.getRolePowerById(rolePowerId), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("getRolePowerByEntry")
	@ResponseBody
	public RetenObj getRolePowerByEntry(RolePower rolePower) {
		try {
			return warpData(rolePowerService.getRolePowerByEntry(rolePower), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("getRolePowersByPage")
	@ResponseBody
	public RetenObj getRolePowersByPage(RolePower rolePower) {
		try {
			return warpData(rolePowerService.getRolePowersByPage(rolePower), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("updateRolePower")
	@ResponseBody
	public RetenObj updateRolePower(RolePower rolePower) {
		try {
			int result = rolePowerService.updateRolePower(rolePower);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	

	@RequestMapping("insertRolePower")
	@ResponseBody
	public RetenObj insertRolePower(RolePower rolePower) {
		try {
			int result = rolePowerService.insertRolePower(rolePower);
			if (result > 0)
				return warpData(rolePower.getRolePowerId(), 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("deleteRolePower")
	
	@ResponseBody
	public RetenObj deleteRolePower(Integer rolePowerId) {
		try {
			int result = rolePowerService.deleteRolePower(rolePowerId);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
